package com.bishe.demo.modules.hotel.hotelMain.dao;

import com.bishe.demo.modules.common.entity.SearchBean;
import com.bishe.demo.modules.hotel.hotelDetail.entity.Detail;
import com.bishe.demo.modules.hotel.hotelMain.entity.HotelMain;
import com.bishe.demo.modules.hotel.hotelPolicy.entity.Policy;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface HotelMainDao {
    @Insert("insert into hotel_main values(#{hotelMainId},#{hotelName},#{province},#{city},#{area},#{longitude},#{latitude},#{location},#{hotelStar})")
    void insertHotelMain(HotelMain hotel);

    @Select("select * from hotel_main where hotel_main_id=#{userId}")
    HotelMain getHotelMainByUserId(int userId);

    @Select("select * from hotel_main")
    @Results(id = "mainResult", value = {
            @Result(column = "hotel_main_id", property = "hotelMainId"),
            @Result(column = "hotel_main_id", property = "hotelDetail",
                    javaType = Detail.class,
                    one = @One(select = "com.bishe.demo.modules.hotel.hotelDetail.dao.DetailDao.getDetailByMainId")),
    })
    List<HotelMain> getHotelMains();

    @Select("select * from hotel_main where province=#{province}")
    @ResultMap(value = "mainResult")
    List<HotelMain> getHotelMainsByProvince(int province);

    @ResultMap(value = "mainResult")
    @Select("select * from hotel_main where city=#{city}")
    List<HotelMain> getHotelMainsByCity(int city);

    @ResultMap(value = "mainResult")
    @Select("select * from hotel_main where area=#{area}")
    List<HotelMain> getHotelMainByArea(int area);

    @Select("select * from hotel_main where hotel_main_id=#{hotelId}")
    @Results(id = "mainResult1", value = {
            @Result(column = "hotel_main_id", property = "hotelMainId"),
            @Result(column = "hotel_main_id", property = "hotelDetail",
                    javaType = Detail.class,
                    one = @One(select = "com.bishe.demo.modules.hotel.hotelDetail.dao.DetailDao.getDetailByMainId")),
            @Result(column = "hotel_main_id", property = "hotelRooms",
                    javaType = List.class,
                    many = @Many(select = "com.bishe.demo.modules.hotel.hotelRoom.dao.RoomDao.getRoomsByHotelId")),
            @Result(column = "hotel_main_id", property = "policy",
                    javaType = Policy.class,
                    one = @One(select = "com.bishe.demo.modules.hotel.hotelPolicy.dao.PolicyDao.getPolicyByHotelId")),
            @Result(column = "hotel_main_id", property = "hotelOrders",
                    javaType = List.class,
                    one = @One(select = "com.bishe.demo.modules.hotel.hotel.dao.HotelOrderDao.getHotelOrdersByHotelMainId")),
    })
    HotelMain getHotelMainByHotelId(int hotelId);

    @Update("update  hotel_main set hotel_name=#{hotelName},province=#{province},city=#{city},area=#{area},longitude=#{longitude},latitude=#{latitude},location=#{location},hotel_star=#{hotelStar} where hotel_main_id=#{hotelMainId}")
    void updateHotelMain(HotelMain hotelMain);

    @Delete("delete from hotel_main where hotel_main_id=#{hotelId}")
    void deleteHotelMainByHotelId(int hotelId);

    @Select("<script>" +
            "select * from hotel_main "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (province = '${keyword}') "
            + " or (city = '${keyword}') "
            + " or (area = '${keyword}') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by hotel_main_id asc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    @ResultMap("mainResult1")
    List<HotelMain> getHotelsBySearchBeans(SearchBean searchBean);

    @Select("select * from hotel_main hm left join hotel_order_hotel_main hohm on hohm.hotel_main_id=hm.hotel_main_id where hohm.hotel_order_id=#{hotelOrderId}")
    @ResultMap("mainResult")
    HotelMain getHotelMainByOrderId(int hotelOrderId);

    @Select("SELECT * FROM hotel_main LIMIT 0, 4")
    @ResultMap("mainResult")
    List<HotelMain> getHotelMainsForMain();
}
